package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentDepartmentIntroductionBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.KEY_DEPARTMENT_CODE
import com.compass.mobile.framework.constant.KEY_DEPARTMENT_ID
import com.compass.mobile.framework.constant.KEY_TYPE
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.DepartmentBean
import com.compass.mobile.models.Doctor
import com.compass.mobile.presenters.DoctorPresenter
import com.compass.mobile.ui.activity.DoctorHomePageActivity
import com.compass.mobile.ui.activity.DoctorRegisterHomeActivity
import com.compass.mobile.ui.viewModel.InquiryViewModel
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 问诊科室首页
 * Date:2024/5/23 9:27
 */
class DepartmentIntroductionFragment : BaseVBFragment<FragmentDepartmentIntroductionBinding>() {
    private var mPage = 1
    private var isHaveNext = true
    private var departmentId: String? = null//科室id
    private var code: String? = null//科室code
    private var comeType: String? = null

    private val inquiryViewModel: InquiryViewModel by viewModels()
    private var doctors: MutableList<Doctor> = mutableListOf()
    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    override fun initView(view: View, savedInstanceState: Bundle?) {
        initSpecialColumn()
        arguments?.let {
            departmentId = it.getString(KEY_DEPARTMENT_ID)
            code = it.getString(KEY_DEPARTMENT_CODE)
            comeType = it.getString(KEY_TYPE)
        }
        FocusViewUtils.scaleView(mBinding?.tvPrevious, 1)
        FocusViewUtils.scaleView(mBinding?.tvNextPage, 1)
    }

    override fun initData() {

        inquiryViewModel.geDeptDetails(departmentId)
        inquiryViewModel.departmentLiveData.observe(this) {
            it?.let {
                setDepartmentView(it)
            }

        }
        getDoctor()
        inquiryViewModel.doctorsLiveData.observe(this) {

            dismissLoading()

            if (mPage == 1) {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    mBinding?.emptyView?.visible()
                    mBinding?.llBelow?.gone()
                } else {
                    isHaveNext(it.size)
                    mBinding?.emptyView?.gone()
                    mBinding?.llBelow?.visible()
                    doctors = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, doctors)
                }
            } else {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    mPage-- //恢复上一次的page
                    showToast("没有下一页了")
                } else {
                    isHaveNext(it.size)

                    doctors = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, doctors)
                }
            }


        }
    }

    private fun getDoctor() {
        showLoading()
        when (comeType) {
            //问诊
            "inquiry" -> {
                inquiryViewModel.getInquiryDoctors(departmentId, pageNo =  mPage)
            }
            //预约
            "booking" -> {
                inquiryViewModel.geRecommendDoctors(code, pageNo = mPage)
            }
        }

    }

    private fun isHaveNext(number: Int) {
        if (number < 4) {
            isHaveNext = false
        } else {
            isHaveNext = true
        }
    }


    private fun setDepartmentView(departmentBean: DepartmentBean) {
        mBinding?.tvDepartmentName?.text = "/${departmentBean.name}"
        mBinding?.tvIntroduce?.text = departmentBean.introduce
        mBinding?.tvConsultationServiceNum?.text = departmentBean.consultationServiceNum
        var number =
            departmentBean.textColumnJournalismNum + departmentBean.videoColumnJournalismNum
        mBinding?.tvCoupeNumber?.text = number.toString()

    }

    override fun intListener() {
        mBinding?.tvPrevious?.onClick {
            if (mPage > 1) {
                mPage--
                getDoctor()
            } else {
                showToast("没有上一页了")
            }

        }

        mBinding?.tvNextPage?.onClick {
            if (isHaveNext) {
                mPage++
                getDoctor()
            } else {
                showToast("没有下一页了")
            }

        }
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val item = doctors[position]
            departmentId?.let {

                //
                when (comeType) {
                    //问诊
                    "inquiry" -> {
                        DoctorHomePageActivity.start(requireContext(), item.id, it)
                    }
                    //预约
                    "booking" -> {
                        DoctorRegisterHomeActivity.start(requireContext(), item.id, it)
                    }
                }


            }


        }
    }

    @SuppressLint("RestrictedApi")
    fun initSpecialColumn() {
        mBinding?.caseItem?.setNumColumns(2)
        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)
        val departmentIntroductionPresenter = DoctorPresenter("2")
        arrayObjectAdapter = ArrayObjectAdapter(departmentIntroductionPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )

        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, doctors)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )
    }


}
